var rowid=null;
var rowpid=null;
var pageSize = 10;
var tranCodeMap = {};
$(document).ready(function(){
	commonSet();
	//表格初始化
	initTable();
	//事件绑定
	eventBind();
});

function commonSet(){
	genDictSelect('transferType','transfer_type','1');
	genDictSelect('transferType','transfer_type1','1');
	tranCodeMap =getTransParam('tranCode');
}

function eventBind(){
	//带有分页信息的查询
	$("#queryBtn1").click(pageQuery).click();
	$("#queryUnBtn").click(pageQuery).click();
	//接诊弹出层
	$("#reBtn").click(showRegFormDialog);
}

function initTable(){
	$('#unfinishTranTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			queryUn(number, size);
		},
		onCheck:function (row) {
			rowid=row.id;
			rowpid=row.patient_id;
		}
	});
	$('#finishTranTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			query(number, size);
		},
		onCheck:function (row) {
			rowid=row.id;
		}
	});
}

//带有分页信息的查询
function pageQuery(){
	queryUn(1, pageSize);
	query(1, pageSize);
};

//我申请的转诊信息
function queryUn(pageNumber, pageSize){
	var queryCondition={
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$("#unfinishForm").collect()
	}
	$.getApiClient('/transfer/mylist').setQuery(queryCondition).post().done(function(data){
	    $('#unfinishTranTable').bootstrapTable('load', data.payload);
	});
};

//我申请的已通过审核的转诊
function query(pageNumber, pageSize){
	var queryCondition={
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$("#finishForm").collect()
	}
	$.getApiClient('/transfer/mylistDone').setQuery(queryCondition).post().done(function(data){
	    $('#finishTranTable').bootstrapTable('load', data.payload);
	});
};

//医生接诊操作
function showRegFormDialog(){
	if(rowid==null){//增加提示
		customTimeOutDialog('信息提示','请选择患者后进行操作',null,2000);
	}else{
		$.getApiClient('/transfer/agree/{id}/{pid}').setQuery({id:rowid,pid:rowpid}).post().done(function(data){
			if(data.code == "0"){
				BootstrapDialog.closeAll();
				customTimeOutDialog('信息提示','接诊成功',null,2000);
				pageQuery();
			}else{
				customAlertDialog(data.message, '错误提示', null, null);
			}
		});
	}
}

//操作列
function operateFormatter(value, row, index) {	
	var operates=[];
	if(row.agreeStatus=='2'){
		operates.push('<a class="edit" href="javascript:void(0)" title="审核详情"><i class="fa fa-edit"></i>审核详情</a>');
	}
	return operates.join('');
};

operateEvents = {
	'click .edit' : function(e, value, row, index) {
		if(row==null){
			customTimeOutDialog('信息提示','请选择患者',null,2000);
		}
		showEditFormDialog(row);
	}
};

function showEditFormDialog(row){
	customRemoteUrlDialog('审核不通过详情','/page/business/transfer/tranViewReson?tId='+row.id,null);
}
